const express = require("express")
const path = require("path")
const config = require("./config/config")
const app = express()
const router = require("./routes/index");  
const fs = require("fs")

app.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', req.headers.origin || '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization,Origin,Accept,X-Requested-With');
    res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.header('Access-Control-Allow-Credentials', true);
    res.header('X-Powered-By', ' 3.2.1');
    res.header('Content-Type', 'application/json;charset=utf-8');
    if (req.method === 'OPTIONS') {
        res.sendStatus(200);
    } else {
        next();
    }
});

const bodyParser=require("body-parser");

// 解析以 application/json 和 application/x-www-form-urlencoded 提交的数据
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())
//配置路由
router(app)


//注册静态文件
app.use(express.static(path.join(__dirname, 'public')));
//注册404
app.use(function(req, res, next) {var err = new Error('Not Found');err.status = 404;next(err);
});
app.listen(config.host.port,(res)=>{
    console.log("监听地址"+config.host.url+":"+config.host.port)
})